var $postPersonnelChangeList = $('#postPersonnelChangeList');

$(function(){
	$postPersonnelChangeList.jqGrid({
			url:  POST_PERSONNEL_CHANGE_PATH + 'postPersonnelChangeListPage.do',
			datatype: "json",
			colNames: ['主键', '系列产品ID', '产品简称', '管理人ID', '管理人名称',
			           '变更岗位', '岗位CODE', '', '岗位层级', '变更前的岗位人员', '变更后的岗位人员', 
			           '变更生效日', '进度状态', '备注', '操作'],
			colModel: [
					  {name: 'id', index:'id', hidden:true, key:true},
					  {name: 'prjCode', index:'prjCode', hidden:true},
					  {name: 'prjSName', index:'prjSName', width:80, align:'left',formatter:POST_PERSONNEL_CHANGE.showPrjDetail},
					  {name: 'companyId', index:'companyId', hidden:true},
					  {name: 'companyName', index:'companyName', width:80, align:'left',formatter:POST_PERSONNEL_CHANGE.showCompanyDetail},
					  {name: 'postTitle', index: 'postTitle', width:80, align:'left'},
					  {name: 'postCode', index: 'postCode', hidden:true},
					  {name: 'postRank', index: 'postRank', hidden:true},
					  {name: 'postRankNm', index: 'postRankNm', width:40, align:'left', hidden:true},
					  {name: 'oldPersonnelNm', index:'oldPersonnelNm', width:80, align:'left'},
					  {name: 'newPersonnelNm', index:'newPersonnelNm', width:80, align:'left'},
					  {name: 'effctiveDate', index:'effctiveDate', width:50, align:'left'},
					  {name: 'handleStatusNm', index:'handleStatusNm', width:50, align:'left'},
					  {name: 'remark', index:'remark', width:80, align:'left'},
					  {name: 'option', index:'option', width:40, align:'left'}
					  ],
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: {
        	        search: "search", 
        	        page: "pageNo",
        	        rows: "limit" 
        	       },
        height: 'auto',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },
        multiselect: false,
        pager: "#postPersonnelChangePage",
        viewrecords: true,
        hidegrid: false,
        subGrid: false,
        gridComplete: function() {
			var ids = $postPersonnelChangeList.jqGrid('getDataIDs');
			for (var i = 0; i < ids.length; i++) {
				var id = ids[i];
				var rowData = $postPersonnelChangeList.jqGrid('getRowData', id);
				var postRank = rowData.postRank;
				var postCode = rowData.postCode;
				if (codes.indexOf("post_" + postCode + "_edit") >= 0) {
					var params = {
							'edit' : 'updatePostPersonnelChangeTemplate',
							'del' : 'deletePostPersonnelChangeTemplate'
					};
					var permissionButton = Permission_Button.configPermissionButton(params);
					
					// 修改
					//permissionButton.edit = Permission_Button.clickPermissionButton("updatePostPersonnelChangeTemplate", "POST_PERSONNEL_CHANGE.updatePostPersonnelChange('" + id + "','" + postRank + "')");
					// 删除
					permissionButton.del = Permission_Button.clickPermissionButton("deletePostPersonnelChangeTemplate", "POST_PERSONNEL_CHANGE.deletePostPersonnelChange('" + id + "')");
					$postPersonnelChangeList.jqGrid('setRowData', id, {option : permissionButton.del});
				}
			}
		},
    });
	$postPersonnelChangeList.navGrid('#postPersonnelChangePage', {edit: false, add: false, del: false, search: false, refreshstate: 'current'});
	$postPersonnelChangeList.jqGrid('setFrozenColumns');
    jqGridResize($postPersonnelChangeList);
});

var POST_PERSONNEL_CHANGE = {
	postPersonnelChangeAddView: function () {
		var actionUrl = POST_PERSONNEL_CHANGE_PATH + "postPersonnelChangeAddView.do";
		window.open(actionUrl);
	},
	showPrjDetail: function(text, options, rData){
 		var prjCode = rData["prjCode"];
 		var actionUrl = PRJ_PATH + "singleProductOverview.do?prjcode=" + prjCode;
 		return prjCode == null ? "" : getHref(text, "POST_PERSONNEL_CHANGE.openView('"+ actionUrl +"')");
	},
	showCompanyDetail: function(text, options, rData){
		var companyId = rData["companyId"];
		return companyId == null ? "" : text;
	},
	openView: function(url){
		window.open(url);
	},
	deletePostPersonnelChange: function (id) {
		ctools.confirm("是否确认删除该变更记录？",function(){
			$.ajax({
				url: POST_PERSONNEL_CHANGE_PATH + "deletePostPersonnelChange.do",
				type: "POST",
				dataType: "JSON",
				data: {"id": id},
				success: function(data) {
					if (data.success) {
						ctools.alert(data.message,"","success");
						queryByCondtion(false);
					}
				},
				error: function(xhr,st,err) {
					ctools.alert("删除出错","","error");
				}
				
			});
		});
	},
	updatePostPersonnelChange: function (id, postRank) {
		var actionUrl = POST_PERSONNEL_CHANGE_PATH + "postPersonnelChangeEditView.do?id=" + id + "&postRank=" + postRank;
		openDialog(actionUrl);
	}
};

//对按钮绑定全屏事件
$(document).on('click', '.btn-fullscreen', function(){
    var $wrapper = $(this).parents('.fullscreen-wrapper')[0];
    toggleFullScreen(document.documentElement);
    //全屏的时候将几个模态框放到下面去
    $('.modal[role="dialog"]').appendTo($wrapper);
});

//按条件查询
function queryByCondtion (flag, parameter) {
	
	var $startUpTitle = $.trim($("#q-startUpTitle").val());
	var $prjSName = $.trim($("#q-prjSName").val());
	var $companyName = $.trim($("#q-companyName").val());
	var $subFundSname = $.trim($("#q-subFundSname").val());
	var $postId = $("#q-postId").val();
	var $deptId = $("#q-deptId").val();
	var $personnelId = $.trim($("#q-personnelId").val());
	var $effctiveDateStart = $("#q-effctiveDate").getDateRangeValue()[0];
    var $effctiveDateEnd = $("#q-effctiveDate").getDateRangeValue()[1];
	
	$personnelId = $personnelId.replace(",", "\|");
	
    var postData = $postPersonnelChangeList.jqGrid("getGridParam", "postData");
    $.extend(postData,{
    	'sp[startUpTitle]': $startUpTitle,
    	'sp[prjSName]': $prjSName,
    	'sp[companyName]': $companyName,
    	'sp[subFundSname]': $subFundSname,
    	'sp[postId]': $postId,
    	'sp[deptId]': $deptId,
    	'sp[personnelId]': $personnelId,
    	'sp[effctiveDateStart]': $effctiveDateStart,
    	'sp[effctiveDateEnd]': $effctiveDateEnd
    });

    if (flag) {
    	$postPersonnelChangeList.jqGrid('setGridParam',{"datatype": "json"}).trigger("reloadGrid",[{page:1}]);//重新载入Grid表格
    } else {
    	$postPersonnelChangeList.jqGrid('setGridParam',{"datatype": "json"}).trigger("reloadGrid");//重新载入Grid表格
    }
}


